//
//  ServiceTermsViewController.m
//  Arts1
//
//  Created by Mac Jc on 15/9/24.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "ServiceTermsViewController.h"
#import "AllHeader.h"

@interface ServiceTermsViewController ()
@property (nonatomic, strong) NSString *serviceTermString;
@property (nonatomic ,strong) UIScrollView *scrollview;
@property (nonatomic ,strong) UILabel *labelTxt;
@property (nonatomic ,strong) UIView *topShadowView;
@end

@implementation ServiceTermsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //  服务条款控制器
    self.view.backgroundColor = kBackgroundColor;
    self.navigationItem.title = @"服务条款";
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//    self.tabBarController.tabBar.hidden = YES;
    [self creactView];
    [self requestTxt];
}

/**
 *  创建视图
 */
- (void)creactView
{
    _scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollview.backgroundColor = kBackgroundColor;
    _scrollview.contentSize = CGSizeMake(0, self.view.height);
    _scrollview.alwaysBounceVertical = YES;
    
    _topShadowView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, SCREEN_HEIGHT)];
    _topShadowView.backgroundColor = [UIColor whiteColor];
    _topShadowView.layer.cornerRadius = 5;
    //    [UIView viewWideForShadowColor:topShadowView];
    

    
    _labelTxt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _topShadowView.width, _topShadowView.height)];
    _labelTxt.font = [UIFont systemFontOfSize:11];
    _labelTxt.textColor = [UIColor grayColor];
    _labelTxt.numberOfLines = 0;
    
    UIImageView *mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 10)];
    mainImageView.backgroundColor = [UIColor whiteColor];
    mainImageView.image = [UIImage imageNamed:@"colorImage"];
    [UIView viewRediusForLeftAndReightOfTop:mainImageView withRedius:5];

    [_scrollview addSubview:_topShadowView];
    [_topShadowView addSubview:_labelTxt];
    [_scrollview addSubview:mainImageView];
    [self.view addSubview:_scrollview];
    
    
    
}

- (void)requestTxt
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"serviceTerms" ofType:@"txt"];
    NSString *string = [[NSString  alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",string);
    
    CGFloat height = [ServiceTermsViewController heightWithString:string];
    _scrollview.contentSize = CGSizeMake(0, height + 50);
    _topShadowView.frame = CGRectMake(10, 10, SCREEN_WIDTH -20, height + 50);
    _labelTxt.frame = CGRectMake(8, 0, _topShadowView.width - 16, height);
    
    _labelTxt.text = string;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_labelTxt.text];;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:8];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, _labelTxt.text.length)];
    
    _labelTxt.attributedText = attributedString;
    
    
//    NSURL *url = [NSURL URLWithString:path];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:1.0];
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//       
//        NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",result);
//    }];
}

+ (CGFloat)heightWithString:(NSString *)string
{
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 50, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.5] forKey:NSFontAttributeName] context:nil];
//    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:string];
//    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
//    [paragraphStyle1 setLineSpacing:8];
    //调节高度
    
    return bounds.size.height;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
